package net.ib.rpc.util;

import java.util.ArrayList;
import java.util.List;

import net.ib.mn.protocol.CommonEnvelope.MethodId;

import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.MethodDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;

public class MethodIdGenerator {

	public static List<MethodDescriptor> getMethodDescriptors(
			FileDescriptor fileDescritor) {
		ArrayList<MethodDescriptor> methodDescList = new ArrayList<MethodDescriptor>();
		for (ServiceDescriptor service : fileDescritor.getServices()) {
			methodDescList.addAll(service.getMethods());
		}
		return methodDescList;
	}

	public static int getMethodId(MethodDescriptor methodDescriptor)
			throws SysException {
		MethodId methodID = MethodId.newBuilder().setVersion(1)
				.setServiceName(methodDescriptor.getService().getName())
				.setMethodName(methodDescriptor.getName()).build();
		return 0x7FFFFFFF & IdGenerator.getId(methodID.toByteArray());
	}
}
